package com.mxchip.livestar.ui.network.wifi.view


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.mxchip.livestar.R
import com.mxchip.livestar.ui.network.wifi.adapter.WifiAdapter
import com.mxchip.livestar.ui.network.wifi.adapter.base.i.OnItemClickListener
import com.mxchip.livestar.ui.network.wifi.widget.ConnectDialog
import com.mxchip.livestar.ui.network.wifi.widget.ConnectedDialog
import com.mxchip.livestar.ui.network.wifi.widget.NoPassDialog
import com.mxchip.livestar.ui.network.wifi.widget.SavedDialog
import com.mxchip.livestar.ui.network.wifimanager.*
import com.videohigh.hxb3288a.ui.network.wifi.view.WLANViewModel
import kotlinx.android.synthetic.main.fragment_wlan.*


class WLANFragment : Fragment(), IWifiView, OnWifiChangeListener, OnWifiConnectListener, OnWifiStateChangeListener {

    private val viewModel by viewModels<WLANViewModel>()

    override fun onDestroyView() {
        super.onDestroyView()
        manager.destroy()
    }

    val manager by lazy {
        WifiManager.create(context?.applicationContext)
    }
    val adapter by lazy {
        WifiAdapter()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_wlan, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view_switch.setOnClickListener {
            viewModel.wifiOpen.value?.let { viewModel.wifiOpen(!it) }
        }
        viewModel.wifiOpen.observe(viewLifecycleOwner, Observer {
            if (it) {
                openWifi()
            } else {
                closeWifi()
            }
        })

        rc_view.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        rc_view.adapter = adapter
        refresh.setOnRefreshListener {
            manager.scanWifi()
            refresh.finishRefresh(500)
        }
        refresh.setEnableLoadMore(false)
        manager.setOnWifiChangeListener(this)
        manager.setOnWifiConnectListener(this)
        manager.setOnWifiStateChangeListener(this)
        adapter.setOnRecyclerViewListener(OnItemClickListener<IWifi> { wifi ->
            if (wifi.isConnected) ConnectedDialog(context)
                    .content(wifi.name())
                    .setOnConnectedDialogListener {
                        manager.disConnectWifi(wifi)
                        refresh.finishRefresh(500)
                    }
                    .show()
            else if (wifi.isSaved) SavedDialog(context)
                    .content(wifi.name())
                    .setOnConnectDialogListener { manager.connectSavedWifi(wifi) }
                    .setOnClearDialogListener { manager.removeWifi(wifi) }
                    .show()
            else if (!wifi.isEncrypt) NoPassDialog(context)
                    .content(wifi.name())
                    .setOnConnectDialogListener { manager.connectOpenWifi(wifi) }
                    .show()
            else ConnectDialog(context)
                    .setOnConnectDialogListener { password -> manager.connectEncryptWifi(wifi, password) }
                    .show()
        } as OnItemClickListener<IWifi?>?)

        viewModel.wifiOpen(manager.isOpened)
    }

    private fun closeWifi() {
        switch1.isChecked = false
        refresh.visibility = View.GONE
        manager.closeWifi()
    }

    private fun openWifi() {
        manager.openWifi()
        switch1.isChecked = true
        refresh.visibility = View.VISIBLE
        manager.scanWifi()
        refresh.finishRefresh(500)

    }

    override fun onWifiChanged(wifis: MutableList<IWifi>?) {
        adapter.bindData(wifis)
    }

    override fun onConnectChanged(status: Boolean) {
//        viewModel.wifiOpen(status)
        // MyLog.logD("WLANFragment::onConnectChanged: status:${status}  ")

    }

    override fun onStateChanged(state: State) {
        // MyLog.logD("WLANFragment::onStateChanged: state:${state.ordinal}  ")
        if (state == State.DISABLED)
            viewModel.wifiOpen(false)
        else if (state == State.ENABLED)
            viewModel.wifiOpen(true)
    }


}
